﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ServerOptions.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The server options.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


namespace DbDiff.Common.DataAccess
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using Helper;
    using Model;

    /// <summary>
    /// The server options.
    /// </summary>
    public class ServerOptions : DatabaseInfoExtended
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ServerOptions"/> class.
        /// </summary>
        /// <param name="databaseObjectTypes">
        /// The dataset name.
        /// </param>
        /// <param name="databaseInfoBase">
        /// The database infobase obj.
        /// </param>
        public ServerOptions(DatabaseObjectTypes databaseObjectTypes, DatabaseInfoBase databaseInfoBase) : base(databaseObjectTypes, databaseInfoBase)
        {
        }

        /// <summary>
        /// The fill data set.
        /// </summary>
        /// <param name="schema">
        /// The schema.
        /// </param>
        /// <param name="table">
        /// The table.
        /// </param>
        /// <exception cref="Exception">
        /// </exception>
        public override void FillDataSet(string schema, string table)
        {            
                using (var connection = new SqlConnection(this.DatabaseInfoBase.ConnectionString))
                {
                    using (var dataAdapter = new SqlDataAdapter())
                    {
                        dataAdapter.SelectCommand = new SqlCommand(this.SqlCommandString(), connection);
                        dataAdapter.SelectCommand.Parameters.Add("@ProcSchema", SqlDbType.NVarChar, 400, "ProcSchema").Value = schema;
                        dataAdapter.SelectCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 400, "Name").Value = table;
                        dataAdapter.SelectCommand.CommandTimeout = this.QueryTimeOut;
                        connection.Open();
                        dataAdapter.MissingMappingAction = MissingMappingAction.Passthrough;
                        dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
                        dataAdapter.Fill(this.DatabaseInfoBase.DatabaseDataSets, DatabaseObjectDictionary.ObjectTypes[this.DatabaseObjectTypes]);
                    }
                    connection.Close();
            }
        }
    }
}